package cn.edu.hit.education.model;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import cn.edu.hit.education.pojo.Course;
import cn.edu.hit.education.pojo.Process;
import cn.edu.hit.education.pojo.Property;
import cn.edu.hit.education.pojo.Specialty;
import cn.edu.hit.education.pojo.University;
import cn.edu.hit.education.pojo.User;
import cn.edu.hit.education.service.ICourseService;
import cn.edu.hit.education.service.IProcessService;
import cn.edu.hit.education.service.IPropertyService;
import cn.edu.hit.education.service.ISpecialtyService;
import cn.edu.hit.education.service.IUniversityService;
import cn.edu.hit.education.service.IUserSpeciatyService;
import cn.edu.hit.education.service.SpecialtyServiceImpl;
import educationschedule.Application;

public class CourseRootNode extends Node {

	IProcessService processService = (IProcessService) Application.applicationContext
			.getBean("processServiceImpl");
	IPropertyService propertyService = (IPropertyService) Application.applicationContext
			.getBean("propertyServiceImpl");
	IUniversityService universityService = (IUniversityService) Application.applicationContext
			.getBean("universityServiceImpl");
	ICourseService courseService = (ICourseService) Application.applicationContext
			.getBean("courseServiceImpl");

	static ISpecialtyService specialtyService = (ISpecialtyService) Application.applicationContext
			.getBean(SpecialtyServiceImpl.class);
	static IUserSpeciatyService userSpeciatyService = (IUserSpeciatyService) Application.applicationContext
			.getBean("userSpeciatyServiceImpl");

	private static CourseRootNode root = null;

	private static List<Specialty> specialties;

	private CourseRootNode() {
	}

	public static synchronized CourseRootNode getRootNode() {
		if (root == null) {
			root = new CourseRootNode();
			//initSpecialties();
			root.setParent(null);
			//root.initRootNode(getSpecialtiesIds());
			root.initRootNode(1);
		}
		return root;

	}

	private static void initSpecialties() {
		Subject subject = SecurityUtils.getSubject();
		// 角色是校长，可查看表中所有专业对应的课程信息
		if (subject.hasRole("principal")) {
			specialties = specialtyService.queryAllSpecialty();
			// 角色是院长，专业初始化为本学院下的所有专业！
		} else if (subject.hasRole("president")) {
			User user = (User) subject.getPrincipal();
			// 根据院长的用户Id查询到关联的专业Id
			List<Integer> ids = userSpeciatyService.querySpeciatyIdsByUser(user.getId());
			specialties = specialtyService.queryRelativeSpecialty(ids);
		} else if (subject.hasRole("director")) {
			User user = (User) subject.getPrincipal();
			// 根据普通用户的用户Id查询到关联的专业Id
			List<Integer> ids = userSpeciatyService.querySpeciatyIdsByUser(user.getId());
			specialties = specialtyService.queryRelativeSpecialty(ids);
		}

	}

	private static List<Integer> getSpecialtiesIds() {

		// 获取当前可查看专业的Id
		List<Integer> specityIds = specialties.stream().map(item -> {
			return item.getId();
		}).collect(Collectors.toList());
		return specityIds;
	}

	private void initRootNode(List<Integer> specialtiesIds) {
		ResourceNode resourceNode = new ResourceNode(0,"教育资源");
		root.addChild(resourceNode);
		
		List<Process> processes = processService.queryAllProcess();
		if (processes != null && processes.size() > 0) {
			for (Process process : processes) {
				ProcessNode processNode = new ProcessNode(process.getId(),process.getName());
				resourceNode.addChild(processNode);

				List<Property> properties = propertyService.queryPropertiesByProcessId(process.getId());
				if (properties != null && properties.size() > 0) {
					for (Property property : properties) {
						PropertyNode propertyNode = new PropertyNode(property.getId(), property.getName());
						processNode.addChild(propertyNode);

						List<Course> courses = courseService.queryCoursesByPropertyId(property.getId());
						
						courses = courses.stream().filter(item->{
							return specialtiesIds.contains(item.getSpecialtyId());
						}).collect(Collectors.toList());

						if (courses != null && courses.size() > 0) {
							for (Course course : courses) {
								CourseNode courseNode = new CourseNode(course.getId(), course.getName());
								propertyNode.addChild(courseNode);
							}
						}
					}
				}

			}
		}
	}
	
	private void initRootNode(int specialtieId) {
		ResourceNode resourceNode = new ResourceNode(0,"教育资源");
		root.addChild(resourceNode);
		
		List<Process> processes = processService.queryAllProcess();
		if (processes != null && processes.size() > 0) {
			for (Process process : processes) {
				ProcessNode processNode = new ProcessNode(process.getId(),process.getName());
				resourceNode.addChild(processNode);

				List<Property> properties = propertyService.queryPropertiesByProcessId(process.getId());
				if (properties != null && properties.size() > 0) {
					for (Property property : properties) {
						PropertyNode propertyNode = new PropertyNode(property.getId(), property.getName());
						processNode.addChild(propertyNode);

						List<Course> courses = courseService.queryCoursesBySpecialtyPropertyId(specialtieId, property.getId());

						if (courses != null && courses.size() > 0) {
							for (Course course : courses) {
								CourseNode courseNode = new CourseNode(course.getId(), course.getName());
								propertyNode.addChild(courseNode);
							}
						}
					}
				}

			}
		}
	}
}
